package atguigu.com.edu.app.dwd.log;

import atguigu.com.edu.util.MyKafkaUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.OutputTag;

import java.util.List;
import java.util.Map;

public class DwdTrafficUserJumpDetail {
    public static void main(String[] args) throws Exception {
        //基本环境准备
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        env.setParallelism(4);
        //设置检查点
        //开启检查点
       /* env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        //设置超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60000L);
        //设置job取消后检查点是否保留
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        //设置检查点最小时间间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000L);
        //设置重启策略
        env.setRestartStrategy(RestartStrategies.failureRateRestart(3, Time.days(30),Time.seconds(3)));
        //设置状态后端
        env.setStateBackend(new HashMapStateBackend());

        env.getCheckpointConfig().setCheckpointStorage("hadoop");
        System.setProperty("ha","da");*/

        String topic="dwd_traffic_page_log";
        String groupId ="dwd_traffic_user_jump_group";

        FlinkKafkaConsumer<String> kafkaConsumer = MyKafkaUtil.getKafkaConsumer(topic, groupId);

        DataStreamSource<String> kafkaStrDs = env.addSource(kafkaConsumer);

        SingleOutputStreamOperator<JSONObject> jsonjDs = kafkaStrDs.map(JSON::parseObject);

       //  jsonjDs.print(">>>>>");
        SingleOutputStreamOperator<JSONObject> withWatermarDs = jsonjDs.assignTimestampsAndWatermarks(WatermarkStrategy
                .<JSONObject>forMonotonousTimestamps()
                .withTimestampAssigner(
                        new SerializableTimestampAssigner<JSONObject>() {
                            @Override
                            public long extractTimestamp(JSONObject jsonObj, long l) {
                                return jsonObj.getLong("ts");
                            }
                        }
                ));

        KeyedStream<JSONObject, String> keyedDs
                = withWatermarDs.keyBy(jsonObj -> jsonObj.getJSONObject("common").getString("mid"));


        Pattern<JSONObject, JSONObject> pattern = Pattern.<JSONObject>begin("first").where(
                new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) {
                        String lastPageId = null;
                        try {
                            lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        } catch (Exception e) {

                        } finally {
                        }
                        return StringUtils.isNotEmpty(lastPageId);
                    }
                }
        ).next("second").where(
                new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObject) {
                        String lastId = null;
                        try {
                            lastId = jsonObject.getJSONObject("page").getString("last_page_id");
                        } catch (Exception e) {

                        } finally {
                        }
                        return StringUtils.isEmpty(lastId);
                    }
                }
        ).within(Time.seconds(3));

        PatternStream<JSONObject> pattern1 = CEP.pattern(keyedDs, pattern);

        OutputTag<JSONObject> timeoutTag = new OutputTag<JSONObject>("timeoutTag"){};

        SingleOutputStreamOperator<JSONObject> matchDs = pattern1.select(
                timeoutTag,
                new PatternTimeoutFunction<JSONObject, JSONObject>() {
                    @Override
                    public JSONObject timeout(Map<String, List<JSONObject>> map, long l) throws Exception {
                        return map.get("first").get(0);
                    }

                }, new PatternSelectFunction<JSONObject, JSONObject>() {
                    @Override
                    public JSONObject select(Map<String, List<JSONObject>> map) throws Exception {
                        return map.get("first").get(0);
                    }
                }
        );



        DataStream<JSONObject> timeoutDs = matchDs.getSideOutput(timeoutTag);
        DataStream<JSONObject> union = matchDs.union(timeoutDs);
        union.print(">>>");
        union.map(jsonObj ->jsonObj.toJSONString() )
                .addSink(MyKafkaUtil.getKafkaProducer("dwd_traffic_user_jump_detail"));

        env.execute();
    }
}
